#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=20;

int plate[2][maxn+1];
long long pow3[maxn+1], cnt[2];

void solve_normal(int s, int t, int p, int n, int k) {
	if (n) {
		solve_normal(s, p, t, n-1, k);
		if (cnt[0]==k) {
			return;
		}

		cnt[0]++;
		plate[0][n] = t;
		if (cnt[0]==k) {
			return;
		}

		solve_normal(p, t, s, n-1, k);
	}
}

void solve_w(int s, int t, int p, int n, int check) {
	if (n) {
		if (check>n) {
			for (int i=1; i<=n; i++) {
				plate[1][i] = t;
			}
			cnt[1]+=pow3[n]-1;
		} else if (check==n) {
			if (plate[1][n]==plate[0][n]) {
				check--;
			}
			solve_w(s, t, p, n-1, check);
			if (check<n) {
				return;
			}

			cnt[1]++;
			plate[1][n] = p;
			if (plate[1][n]==plate[0][n]) {
				check--;
			}
			solve_w(t, s, p, n-1, check);
			if (check<n) {
				return;
			}

			cnt[1]++;
			plate[1][n] = t;
			if (plate[1][n]==plate[0][n]) {
				check--;
			}
			solve_w(s, t, p, n-1, check);
		}
	}
}

int main() {
#ifdef DEBUG
	freopen("src.in", "r", stdin);
	freopen("src.out", "w", stdout);
#endif

	int n, k;
	scanf("%d %d", &n, &k);

	cnt[0]=cnt[1]=0;
	pow3[0] = 1;
	for (int i=1; i<=n; i++) {
		plate[0][i] = plate[1][i] = 1;
		pow3[i] = pow3[i-1]*3;
	}
	solve_normal(1, 3, 2, n, k);
	solve_w(1, 3, 2, n, n);

	printf("%lld ", cnt[1]);

	fclose(stdin);
	fclose(stdout);
	return 0;
}
